﻿using System;
using System.Data;

public class ExecuteRule
{
    public void BillSelectedRuleCycle(int Rule, String CycleDates)
    {
        String query = "SELECT f.RowNo, f.Name, f.WhenPart, f.ThenPart, f.GroupByPart FROM Formula f, RuleRelations r where r.SubRuleId = f.RowNo ";
        query += " AND r.RuleId = '" + Rule.ToString() + "' ORDER BY r.Priority";

        //generate where clause for dates
        DateTime fromDate = Convert.ToDateTime(CycleDates.Split('-')[0]), toDate = Convert.ToDateTime(CycleDates.Split('-')[1]);
        String datePart = " AGREEMENTDATE >= " + fromDate.ToString(GlobalVariables.DateFormat);
        datePart += " AND AGREEMENTDATE <= " + toDate.ToString(GlobalVariables.DateFormat);

        DataTable dt = DataBaseMethods.ExecuteSelectQuery(query);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            String then = dt.Rows[i]["ThenPart"].ToString();
            String when = dt.Rows[i]["WhenPart"].ToString();
            String groupBy = dt.Rows[i]["GroupByPart"].ToString();
            when += datePart;

            //execute each subrule
            executeSubRule(then, when, groupBy);
        }
        return;
    }

    private void executeSubRule(String select, String where, String groupBy)
    {
        return;
    }
}
